Repository navigation
ToCString: word-align stackalloc buffers - #599
miriampolzer wants to merge 1 commit into
Conversation
|
@andres-erbsen thoughts on this? I wasn't sure if we want the default to be aligned or not-aligned. Now the default is aligned, but we could also have a switch inside bedrock2 at a level we choose to emit aligned or not-aligned stackallocs. E.g. at notation level, having two different notations for aligned or non-aligned allocations, or at printing level, saying if we want byte arrays aligned or not... Given the fact that this issue occurs between our own bedrock2 code and our own fiat generated code, I think we should fix it here and not just in boringssl. But let me know if you think this is something to be added manually afterwards. Another question is if we want to emit the |
fe7924d to
3e96018
Compare
Printed code accesses memory bytewise or through memcpy, but stackalloc'd buffers are often passed to external C that takes word pointers (e.g. fiat-crypto field arithmetic in BoringSSL), where an underaligned uint8_t array is undefined behavior (observed with UBSan on 32-bit) and can fault on strict-alignment targets. The semantics lets stackalloc return any address, so choosing a word-aligned one is a refinement; the RISC-V compiler already does so. The prelude defines BR_STACKALLOC_ALIGN unless it is already defined; code printed with c_func but without the prelude must define it (possibly empty).
3e96018 to
2171b2c
Compare
|
NB: Alignment is necessary, but not sufficient, to pass a Bedrock2 array to a C function. It's still a strict aliasing violation. |
Bedrock2-generated P-256 point operations and their callers in p256.cc.inc passed byte-aligned uint8_t buffers to Fiat field ops that take br_word_t pointers, causing undefined behavior. Word-align these buffers and verify alignment at the Bedrock-to-Fiat boundary by prefixing generated stackallocs with BR_STACKALLOC_ALIGN (matching mit-plv/bedrock2#599), typing the p256.cc.inc scratch buffers as fiat_p256_felem[3], and routing pointer conversions through an asserting br_word_ptr helper with a static alignment check. Bug: 586589239 Change-Id: Ib60cc2f8dbe064b8b270ac686e0b50dc3249a852 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/106848 Reviewed-by: Adam Langley <agl@google.com> Commit-Queue: Miriam Polzer <mpolzer@google.com> Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
Printed code accesses memory bytewise or through memcpy, but stackalloc'd buffers are often passed to external C that takes word pointers. BoringSSL passes them to fiat-crypto field arithmetic (
uint32_t[8]/uint64_t[4]), where an underaligneduint8_tarray is undefined behavior; UBSan caught this on 32-bit builds (https://issues.chromium.org/issues/586589239). It can also fault on strict-alignment targets.The semantics lets stackalloc return any address, so choosing a word-aligned one is a refinement; the RISC-V compiler already does so. The prelude includes
<stdalign.h>and definesBR_STACKALLOC_ALIGNasalignas(br_word_t)unless it is already defined, so users can define it empty to get plain byte arrays. Code printed withc_funcbut without the prelude (e.g. fiat-crypto'ssrc/Bedrock/P256.vprinting for BoringSSL) must define it; BoringSSL will carry the same#ifndefblock.Tested by printing
ToCStringTestandToCStringStackallocLoopTestand building them as C11/C17/C2x/C23 (gcc and clang, x86_64 and i386, aligned and with-DBR_STACKALLOC_ALIGN=, identical results) and as C++11/17/23, and by rebuilding fiat-crypto'ssrc/Bedrock/P256.v,P256/Coord.vandP256/Coord32.vwith this change: the printed C matches what BoringSSL ships, function for function.